#include "../../inc/rand/randombytes.h"

#if REE_OR_TEE == 0

#ifdef _WIN32
#include <windows.h>
#include <wincrypt.h>
#else
#include <fcntl.h>
#include <errno.h>
#ifdef __linux__
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#else
#include <unistd.h>
#endif
#endif

#ifdef _WIN32
void randombytes(uint8_t *out, size_t outlen)
{
    HCRYPTPROV ctx;
    DWORD len;

    if (!CryptAcquireContext(&ctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
        abort();

    while (outlen > 0)
    {
        len = (outlen > 1048576) ? 1048576 : outlen;
        if (!CryptGenRandom(ctx, len, (BYTE *)out))
            abort();

        out += len;
        outlen -= len;
    }

    if (!CryptReleaseContext(ctx, 0))
        abort();
}
#elif defined(__linux__) && defined(SYS_getrandom)
void randombytes(uint8_t *out, size_t outlen)
{
    ssize_t ret;

    while (outlen > 0)
    {
        ret = syscall(SYS_getrandom, out, outlen, 0);
        if (ret == -1 && errno == EINTR)
            continue;
        else if (ret == -1)
            abort();

        out += ret;
        outlen -= ret;
    }
}
#else
void randombytes(uint8_t *out, size_t outlen)
{
    static int fd = -1;
    ssize_t ret;

    while (fd == -1)
    {
        fd = open("/dev/urandom", O_RDONLY);
        if (fd == -1 && errno == EINTR)
            continue;
        else if (fd == -1)
            abort();
    }

    while (outlen > 0)
    {
        ret = read(fd, out, outlen);
        if (ret == -1 && errno == EINTR)
            continue;
        else if (ret == -1)
            abort();

        out += ret;
        outlen -= ret;
    }
}
#endif

#elif REE_OR_TEE == 1 // in TEE

#include "tee_crypto_api.h"
#include <string.h>

uint8_t rand_list[1000] = {
    0x19, 0x4f, 0xb3, 0x53, 0x6b, 0xaa, 0x09, 0x88, 0xac, 0x55, 0xcd, 0x16, 0x78, 0xf2, 0xad, 0xbb, 0x0b, 0xb9, 0xb4, 0x77, 0x96, 0x88, 0x86, 0x47, 0xb4, 
    0x47, 0xbe, 0x55, 0xf9, 0x64, 0xcf, 0xc4, 0x3b, 0x89, 0x41, 0xb7, 0x18, 0x05, 0xf4, 0x5d, 0xb4, 0x26, 0xde, 0x29, 0x83, 0x90, 0x70, 0x72, 0xee, 0x42, 
    0x8d, 0x90, 0xcb, 0x41, 0xf9, 0x98, 0x99, 0x32, 0xcf, 0x9e, 0x2e, 0x1f, 0x7a, 0xdc, 0x8d, 0x4f, 0xfc, 0x70, 0xf1, 0x4b, 0x5a, 0x8d, 0x60, 0x94, 0x09, 
    0x5c, 0xd1, 0xbc, 0x10, 0x4d, 0xd9, 0x20, 0x91, 0x3d, 0x65, 0xa3, 0xe6, 0x0b, 0xa6, 0xd1, 0xd5, 0x45, 0x7c, 0x0d, 0x3e, 0xa6, 0x82, 0xb8, 0x92, 0x3f, 
    0x10, 0x06, 0xb6, 0x83, 0xe8, 0xd4, 0x62, 0x9b, 0xac, 0x70, 0xd1, 0xd4, 0x5e, 0x74, 0x51, 0x32, 0x7f, 0xa6, 0x06, 0x79, 0x7a, 0x00, 0xb5, 0xfe, 0x5d, 
    0x66, 0xaf, 0x32, 0x06, 0x31, 0xa4, 0xf6, 0xcb, 0x24, 0x5d, 0x8f, 0xb8, 0xc8, 0x73, 0x51, 0x99, 0xfd, 0x66, 0xb8, 0x69, 0x0e, 0xb9, 0x7e, 0x26, 0x6a, 
    0xfe, 0x45, 0x17, 0x4f, 0x9a, 0x26, 0x73, 0x08, 0xc5, 0x62, 0xd3, 0x49, 0x56, 0x43, 0x6e, 0x70, 0x2c, 0xef, 0xb4, 0x45, 0x99, 0x86, 0xa5, 0xab, 0xad, 
    0x1a, 0xfb, 0xb7, 0x78, 0x9b, 0xb9, 0xbe, 0xac, 0xe3, 0x7e, 0xf0, 0x47, 0xd6, 0xbf, 0x86, 0x72, 0x91, 0x25, 0x8c, 0x77, 0xf6, 0x6a, 0x77, 0x82, 0x00, 
    0x20, 0xe4, 0x6f, 0x8d, 0x2e, 0x15, 0x40, 0xe4, 0x8a, 0x9c, 0x5f, 0xdd, 0x2f, 0x19, 0x2a, 0x81, 0x37, 0xf1, 0x22, 0x89, 0x02, 0x21, 0x1e, 0x33, 0x6d, 
    0xb2, 0x1e, 0xb2, 0x08, 0xb9, 0x24, 0xaf, 0x09, 0xd8, 0x50, 0xe3, 0xb5, 0x73, 0x88, 0xe7, 0xd4, 0xa6, 0xd8, 0x67, 0x20, 0x64, 0x2b, 0x70, 0x6b, 0xb5, 
    0x8b, 0xee, 0xa5, 0x6c, 0xc0, 0x95, 0xab, 0x93, 0x87, 0xf5, 0xd3, 0x49, 0x2f, 0x89, 0x1e, 0xcb, 0xaa, 0x58, 0x47, 0xa4, 0xc3, 0xa6, 0x90, 0x53, 0xfe, 
    0x57, 0xa9, 0x4a, 0x58, 0x5b, 0xf8, 0x7d, 0x47, 0xd1, 0xbc, 0xa9, 0x43, 0x7c, 0x10, 0xf7, 0x91, 0x03, 0x83, 0x6a, 0xaa, 0xbc, 0x75, 0x1b, 0xd0, 0xe1, 
    0x2c, 0xb3, 0x43, 0x29, 0x2c, 0x92, 0xb0, 0x2c, 0x96, 0x89, 0xc9, 0x8b, 0xe2, 0x54, 0xd4, 0x7e, 0xfa, 0x89, 0x48, 0xd4, 0xaf, 0x3b, 0xbb, 0x44, 0xe3, 
    0x03, 0x30, 0xdb, 0x08, 0xdf, 0x11, 0xb2, 0x27, 0x59, 0xc7, 0xda, 0x0b, 0x50, 0x1d, 0x27, 0x1c, 0x7d, 0x62, 0x76, 0x3b, 0xb9, 0xf2, 0xc3, 0x56, 0x3f, 
    0x7b, 0xa9, 0x26, 0x51, 0x8c, 0xb1, 0x26, 0x68, 0xcb, 0x92, 0xbf, 0xcd, 0xd9, 0xa0, 0x6a, 0xa9, 0x22, 0x3b, 0x77, 0x07, 0x48, 0x0e, 0xc3, 0x33, 0x14, 
    0x23, 0x23, 0xed, 0x49, 0xb8, 0x89, 0x15, 0xa1, 0x53, 0x44, 0x21, 0x90, 0x45, 0x4b, 0x47, 0xdf, 0xa6, 0x56, 0x77, 0xba, 0xaf, 0x83, 0x37, 0xef, 0x31, 
    0xcd, 0xf2, 0xd4, 0x1b, 0xb0, 0x28, 0xbb, 0xe3, 0xf3, 0x7a, 0x9e, 0x81, 0xe0, 0x32, 0xb2, 0x9d, 0x8c, 0xde, 0x2d, 0x99, 0xc6, 0xc7, 0xcc, 0x4c, 0x0c, 
    0x2b, 0x7f, 0xea, 0x5e, 0x48, 0x88, 0x29, 0xcb, 0xc4, 0x3d, 0x3e, 0x14, 0xe1, 0xca, 0x71, 0x65, 0x32, 0x1a, 0xc9, 0xc1, 0xcc, 0xb2, 0x81, 0x94, 0xe1, 
    0x56, 0x5c, 0x2c, 0xf6, 0x94, 0xc2, 0x61, 0x81, 0xdc, 0xd1, 0x68, 0x3a, 0xc3, 0xc1, 0xcd, 0x02, 0xee, 0x39, 0x59, 0x56, 0x14, 0xf9, 0x04, 0x99, 0x36, 
    0xd6, 0xf4, 0x83, 0x0c, 0x5a, 0x08, 0xf0, 0x1e, 0x57, 0x9c, 0x81, 0xec, 0xc6, 0xf8, 0x69, 0xdb, 0xb9, 0x09, 0x06, 0x42, 0xda, 0xaf, 0x2f, 0x55, 0xf1, 
    0xd0, 0xef, 0x2e, 0x6d, 0x53, 0x00, 0x9f, 0x24, 0xd7, 0x35, 0x52, 0xcb, 0x80, 0x46, 0x31, 0xb4, 0xd4, 0x1b, 0x00, 0xdc, 0x1e, 0xb3, 0x30, 0x34, 0x25, 
    0xa7, 0x97, 0x43, 0x6d, 0xf0, 0x05, 0xdb, 0x93, 0xdb, 0x78, 0x60, 0xab, 0x55, 0xfe, 0xe9, 0x33, 0x09, 0x8a, 0x48, 0xe3, 0xcb, 0x67, 0x2a, 0xda, 0x00, 
    0xa2, 0x3d, 0x1c, 0x60, 0xe7, 0x59, 0x93, 0x7c, 0x5a, 0x3d, 0xa7, 0xcb, 0x14, 0x34, 0x79, 0xa4, 0xb9, 0x0f, 0xc6, 0xa6, 0x8a, 0xde, 0xf5, 0xa6, 0x08, 
    0x31, 0x02, 0xa0, 0x8a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0x32, 0xd0, 0xb8, 0x7c, 0x0a, 0x02, 0x2e, 0xc6, 0x8e, 0xb0, 0xe0, 0x71, 0x65, 0x7f, 0x60, 
    0xe0, 0x92, 0xcc, 0xdb, 0x15, 0xb3, 0xb9, 0x9b, 0x7c, 0xa6, 0x6d, 0x31, 0x97, 0x85, 0x2d, 0x88, 0x2d, 0x99, 0xef, 0x6f, 0xc4, 0xd5, 0xa3, 0x45, 0xc8, 
    0x88, 0x63, 0x8c, 0x66, 0xa7, 0xf1, 0x51, 0x58, 0x0b, 0xdc, 0x75, 0x64, 0x54, 0x32, 0xc3, 0x22, 0x77, 0x08, 0x4d, 0xce, 0xb6, 0x36, 0x35, 0x07, 0x7a, 
    0x56, 0x05, 0xdd, 0x7e, 0x4b, 0xc5, 0x7c, 0x34, 0x65, 0x4a, 0x6e, 0x0c, 0xb5, 0xed, 0x6a, 0xe3, 0x21, 0x97, 0xc9, 0x76, 0xfe, 0x7a, 0x5f, 0x37, 0xef, 
    0x9c, 0xfb, 0x82, 0x72, 0x47, 0xc4, 0x38, 0x54, 0x34, 0x76, 0x1f, 0x59, 0xe0, 0x7f, 0x9a, 0x10, 0x73, 0x1c, 0x8e, 0xc9, 0xce, 0x8d, 0x08, 0x17, 0x91, 
    0x9d, 0x11, 0x18, 0x29, 0xb7, 0x64, 0xa8, 0xad, 0x79, 0x74, 0x1f, 0xd8, 0x2e, 0x23, 0xd4, 0x1c, 0xc4, 0x88, 0x60, 0xa0, 0x5f, 0x98, 0x92, 0x23, 0xc8, 
    0x06, 0xc5, 0xb1, 0xaa, 0x8d, 0x5d, 0xd0, 0x07, 0xdd, 0x81, 0xb8, 0x01, 0x69, 0x71, 0x58, 0xc8, 0xeb, 0x9e, 0xcc, 0xff, 0xd7, 0xec, 0x51, 0xd8, 0x5a, 
    0xe7, 0x9c, 0x19, 0x74, 0x2b, 0x37, 0xfb, 0xf9, 0xcc, 0x50, 0x42, 0xe1, 0xea, 0x1a, 0x32, 0x2a, 0xa2, 0xfe, 0xf9, 0xfc, 0xe6, 0x95, 0xb9, 0x89, 0xa9, 
    0x16, 0x4f, 0x27, 0x39, 0xa1, 0x31, 0x7d, 0xf1, 0x06, 0x46, 0x3e, 0x8b, 0x3e, 0x46, 0xb2, 0x2d, 0xfd, 0x28, 0x01, 0x2f, 0x8e, 0xe3, 0x8b, 0x34, 0x6f, 
    0xe6, 0x1d, 0x8c, 0x9f, 0x88, 0xa6, 0x53, 0x0c, 0x40, 0xd5, 0x46, 0xef, 0xfa, 0x91, 0x4d, 0xc6, 0xbd, 0x79, 0x1a, 0x3e, 0x0c, 0x9a, 0x21, 0xba, 0xa7 
};

int pos = 0;

void randombytes(uint8_t *out, size_t outlen)
{
    /*
	 * The TEE_GenerateRandom function is a part of TEE Internal Core API,
	 * which generates random data
	 *
	 * Parameters:
	 * @ randomBuffer : Reference to generated random data
	 * @ randomBufferLen : Byte length of requested random data
	 */
	TEE_GenerateRandom(out, outlen);

    // if (pos+outlen <= sizeof(rand_list)) {
    //     memcpy(out, ((uint8_t*)rand_list) + pos, outlen);
    //     pos += outlen;
    //     if (pos == sizeof(rand_list)) {
    //         pos = 0;
    //     }
    //     return;
    // }

    // memcpy(out, ((uint8_t*)rand_list) + pos, sizeof(rand_list) - pos);
    // outlen -= sizeof(rand_list) - pos;
    // out += sizeof(rand_list) - pos;
    // pos = 0;
    
    // while (outlen > sizeof(rand_list)) {
    //     memcpy(out, (uint8_t*)rand_list, sizeof(rand_list));
    //     out += sizeof(rand_list);
    //     outlen -= sizeof(rand_list);
    // }

    // if (outlen > 0) {
    //     memcpy(out, (uint8_t*)rand_list, outlen);
    //     pos += outlen;
    // }
}

#endif